// pages/cooperate/cooperate.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    typeList: ["请选择", "公会合作", "广告合作", "内容合作", "其他咨询"],
    typeShow: false,
    typeNum: 0,
    coopname: "",
    coopvx: "",
    coopmes: ""
  },
  coopmtypeIpt() {
    this.setData({
      typeShow: !this.data.typeShow
    })
  },
  changeType(data) {
    console.log(data.currentTarget.dataset.index)
    this.setData({
      typeNum: data.currentTarget.dataset.index,
      typeShow: false
    })
  },
  coopnameIpt(data) {
    this.setData({
      coopname: data.detail.value
    })
  },
  coopvxIpt(data) {
    this.setData({
      coopvx: data.detail.value
    })
  },
  coopmesIpt(data) {
    this.setData({
      coopmes: data.detail.value
    })
  },
  subInfo() {
    if (this.data.typeNum) {
      if (this.data.coopname) {
        if (this.data.coopvx) {
          let timestamp = (Date.parse(new Date()) / 1000).toString();
          let _token =
            "wechat=" +
            this.data.coopvx +
            "&userId=" +
            app.globalData.user_id +
            "&type=" +
            this.data.typeNum +
            "&token=" +
            app.globalData.userToken +
            "&time=" +
            timestamp +
            "&realname=" +
            this.data.coopname +
            "&message=" +
            this.data.coopmes +
            "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
          let params = {
            userId: app.globalData.user_id,
            token: app.globalData.userToken,
            realname: this.data.coopname,
            wechat: this.data.coopvx,
            type: this.data.typeNum,
            message: this.data.coopmes,
            time: timestamp,
            sign: md5.hexMD5(_token),
          };
          axios.axios({
            url: "/index/cooperate",
            data: params
          }).then((res) => {
            console.log(res.data),
            wx.showToast({
              title: res.data.text,
              icon:'none'
            })
              this.setData({
                coopname:"",
                typeNum:0,
                coopvx:"",
                coopmes:""
              })
          })
        } else {
          wx.showToast({
            title: '微信不能为空',
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: '姓名不能为空',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '未选择合作类型',
        icon: 'none'
      })
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})